package com.mynews.adapter;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.mynews.R;
import com.mynews.activity.LatestNewsDetailActivity;
import com.mynews.bean.DailyListBean;
import com.mynews.utils.StringUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

import static com.mynews.bean.DailyListBean.CONTENT_DATE_TYPE;

/**
 * Created by Administrator on 2017/6/23.
 */

public class HomeFragmentRecyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final String TAG = HomeFragmentRecyAdapter.class.getSimpleName();
    private Context context;
    private DailyListBean dailyListBean;
    private LayoutInflater inflater;
    private List<DailyListBean.StoriesBean> stories;
    private HomeFragmentViewpagerAdapter homeFragmentViewpagerAdapter;
    private int dx = 0;



    public static final int HEAD1NUM = 0;//下拉刷新头部
    public static final int HEAD2NUM = 1;//viewPager显示头部
    public static final int HEAD1 = -1;
    public static final int HEAD2 = -4;
    public static final int FOOTNUM = 1;
    public static final int FOOT = -2;
    public static final int CONTENT = -3;
    public static final int CONTENT_DATE = -5;



    public void setDx(int dx) {
        this.dx = dx;
        notifyItemChanged(0);
    }

    public HomeFragmentRecyAdapter(Context context) {
        this.context = context;
        inflater = LayoutInflater.from(context);
        stories = new ArrayList<>();
    }

    public void addData(DailyListBean dailyListBean) {
        if (dailyListBean.getStories() != null) {
            this.stories.addAll(dailyListBean.getStories());
            Log.e(TAG, "addData: " + dailyListBean.getStories().toString());
        }
        this.dailyListBean = dailyListBean;
        notifyDataSetChanged();
    }


    @Override
    public int getItemViewType(int position) {
        if (position < HEAD1NUM) {
            return HEAD1;
        } else if (position < HEAD2NUM + HEAD1NUM && position > HEAD1NUM - 1) {
            return HEAD2;
        } else if (position > HEAD2NUM + HEAD1NUM + stories.size() - 1) {
            return FOOT;
        } else {
            int poi = position - HEAD1NUM - HEAD2NUM;
            if (CONTENT_DATE_TYPE.equals(stories.get(poi).getContentType())) {
                return CONTENT_DATE;
            }
            return CONTENT;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case HEAD1:
                View head1 = inflater.inflate(R.layout.item_head_flush, parent, false);
                Head1ViewHolder head1ViewHolder = new Head1ViewHolder(head1);
                return head1ViewHolder;
            case HEAD2:
                View head2 = inflater.inflate(R.layout.item_home_viewpager, parent, false);
                Head2Holder head2Holder = new Head2Holder(head2);
                return head2Holder;
            case CONTENT:
                View content = inflater.inflate(R.layout.item_daily, parent, false);
                ContentHolder contentHolder = new ContentHolder(content);
                return contentHolder;
            case CONTENT_DATE:
                View contentDate = inflater.inflate(R.layout.item_content_date, parent, false);
                ContentDateHolder contentDateHolder = new ContentDateHolder(contentDate);
                return contentDateHolder;
            case FOOT:
                View foot = inflater.inflate(R.layout.item_foot, parent, false);
                FootHolder footHolder = new FootHolder(foot);
                return footHolder;
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ContentHolder) {
            int poi = position - HEAD1NUM - HEAD2NUM;
            DailyListBean.StoriesBean storiesBean = stories.get(poi);
            ContentHolder mHolder = (ContentHolder) holder;
            List<String> images = storiesBean.getImages();
            if (images != null && images.size() > 0) {
                Glide.with(context).load(images.get(0)).centerCrop().into(mHolder.ivDailyItemImage);
            }
            mHolder.tvDailyItemTitle.setText(storiesBean.getTitle());
            mHolder.tvDailyItemTitle.setTag(storiesBean);

        } else if (holder instanceof ContentDateHolder) {
            ContentDateHolder contentDateHolder = (ContentDateHolder) holder;
            int poi = position - HEAD1NUM - HEAD2NUM;
            DailyListBean.StoriesBean storiesBean = stories.get(poi);
            contentDateHolder.dateTextview.setText(StringUtils.toDate(storiesBean.getDate()));
        } else if (holder instanceof Head2Holder) {
            Head2Holder head2Holder = (Head2Holder) holder;
            if (homeFragmentViewpagerAdapter == null) {
                homeFragmentViewpagerAdapter = new HomeFragmentViewpagerAdapter(context, dailyListBean);
                head2Holder.homeFragmentViewpager.setAdapter(homeFragmentViewpagerAdapter);
            }
        } else if (holder instanceof FootHolder) {
            FootHolder footHolder = (FootHolder) holder;
        }
        else if(holder instanceof Head1ViewHolder){
            Head1ViewHolder holder1 = (Head1ViewHolder) holder;
            ViewGroup.LayoutParams layoutParams = holder1.rootLl.getLayoutParams();
            Log.e(TAG, "onBindViewHolder: layoutParams.height:"+layoutParams.height );

            layoutParams.height = dx;

            holder1.rootLl.setLayoutParams(layoutParams);
        }
    }

    @Override
    public int getItemCount() {
        if (stories.size() == 0) {
            return 0;
        }
        return stories.size() + HEAD1NUM + HEAD2NUM + FOOTNUM;
    }


    public class ContentHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.iv_daily_item_image)
        ImageView ivDailyItemImage;
        @Bind(R.id.tv_daily_item_title)
        TextView tvDailyItemTitle;
        @Bind(R.id.ll_click)
        CardView llClick;

        ContentHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
            llClick.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    DailyListBean.StoriesBean storiesBean = (DailyListBean.StoriesBean) tvDailyItemTitle.getTag();
                    List<String> images = storiesBean.getImages();
                    String imgUrl = "";
                    if (images != null && images.size() > 0) {
                        imgUrl = images.get(0);
                    }
                    LatestNewsDetailActivity.start(context, ivDailyItemImage, storiesBean.getId(), imgUrl);
                }
            });
        }
    }

    class Head2Holder extends RecyclerView.ViewHolder {
        @Bind(R.id.home_fragment_viewpager)
        ViewPager homeFragmentViewpager;

        Head2Holder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    class FootHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.load_statue_text)
        TextView loadStatueText;
        @Bind(R.id.ll_statue)
        View llStatue;

        FootHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    class ContentDateHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.date_textview)
        TextView dateTextview;

        ContentDateHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    class Head1ViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.iv_arrow)
        ImageView ivArrow;
        @Bind(R.id.tv_refresh)
        TextView tvRefresh;
        @Bind(R.id.tv_refresh_time)
        TextView tvRefreshTime;
        @Bind(R.id.root_ll)
        View rootLl;

        Head1ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
